package org.teziyou.ticket.util;

import org.teziyou.ticket.model.Dict;
import org.teziyou.ticket.model.Member;
import org.teziyou.ticket.model.OnSaleTicket;
import org.teziyou.ticket.model.ScenicComment;
import org.teziyou.ticket.model.ScenicSpot;
import org.teziyou.ticket.model.Ticket;

import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

public class DbUtil {
	
	public static void init(JFinalConfig config, Plugins me) {
		config.loadPropertyFile("jdbc.txt");
		
		String commodityUrl = config.getProperty("commodity.url");
		String commodityUsername = config.getProperty("commodity.user");
		String commodityPassword = config.getProperty("commodity.password");
//		String commodityDriver = super.getProperty("commodity.driver");
		DruidPlugin commodityDp = new DruidPlugin(commodityUrl, commodityUsername, commodityPassword);
		ActiveRecordPlugin commodityArp = new ActiveRecordPlugin("commodity", commodityDp);
		me.add(commodityDp);
//		C3p0Plugin commodityCp = new C3p0Plugin(commodityUrl, commodityUsername, commodityPassword, commodityDriver);
//		ActiveRecordPlugin commodityArp = new ActiveRecordPlugin("commodity", commodityCp);
//		me.add(commodityCp);
		commodityArp.setDevMode(true);
		commodityArp.setShowSql(true);
		me.add(commodityArp);
		commodityArp.addMapping("ticket_info", "ticket_info_id", Ticket.class);
		commodityArp.addMapping("on_sale_ticket", "on_sale_id", OnSaleTicket.class);
		commodityArp.addMapping("scenic_spot", "scenic_id", ScenicSpot.class);
		commodityArp.addMapping("scenic_comments", "comment_id", ScenicComment.class);
		
		String commonUrl = config.getProperty("common.url");
		String commonUsername = config.getProperty("common.user");
		String commonPassword = config.getProperty("common.password");
//		String commonDriver = super.getProperty("commodity.driver");
		DruidPlugin commonDp = new DruidPlugin(commonUrl, commonUsername, commonPassword);
		ActiveRecordPlugin commonArp = new ActiveRecordPlugin("common", commonDp);
		me.add(commonDp);
//		C3p0Plugin commonCp = new C3p0Plugin(commonUrl, commonUsername, commonPassword, commonDriver);
//		ActiveRecordPlugin commonArp = new ActiveRecordPlugin("common", commonCp);
//		me.add(commonCp);
		commonArp.setDevMode(true);
		commonArp.setShowSql(true);
		me.add(commonArp);
		commonArp.addMapping("dictionary_definition", "DictionaryId", Dict.class);
		
		String memberUrl = config.getProperty("member.url");
		String memberUsername = config.getProperty("member.user");
		String memberPassword = config.getProperty("member.password");
//		String memberDriver = super.getProperty("member.driver");
		DruidPlugin memberDp = new DruidPlugin(memberUrl, memberUsername, memberPassword);
		ActiveRecordPlugin memberArp = new ActiveRecordPlugin("member", memberDp);
		me.add(memberDp);
//		C3p0Plugin memberCp = new C3p0Plugin(memberUrl, memberUsername, memberPassword, memberDriver);
//		ActiveRecordPlugin memberArp = new ActiveRecordPlugin("member", memberCp);
//		me.add(memberCp);
		memberArp.setDevMode(true);
		memberArp.setShowSql(true);
		me.add(memberArp);
		memberArp.addMapping("our_members", "Id", Member.class);
	}

}
